import re

# search(),获取匹配内容group()
print(re.search("[a-z]", '123xx456').group())
print(type(re.search("[a-z]", '123xx456').group()))
# 只匹配一次，所以结果只有一个‘x’

# match()
try:
    print(re.match("[a-z]", '123x456').group())
    print(type(re.match("[a-z]", '123x456').group()))
except Exception as e:
    print("match因为不是从第一位匹配，出错了")

print(re.match("[a-z]", 'xx123456').group())
print(type(re.match("[a-z]", 'xx123456').group()))
# 同样只匹配一次

# findall()
print(re.findall("[a-z]", '123xx456'))
print(type(re.findall("[a-z]", '123xx456')))
# 返回所有匹配，返回list，不用group()获取

# finditer()
res = re.finditer('\w', '123xyz456')
print(type(res))
print(next(res).group())    # next(it)是re.match，用group()访问内容

for i in res:
    print(i.group())
# 返回所有匹配，返回iter

# split()
myStr = "asdas\rd&a\ts12d\n*a3sd@a_1sd"
res1 = re.split("[^a-z]",myStr)     # 匹配不是a~z的任何字符
print(res1)
res1 = re.split("\W",myStr)         # \W相当于[^0-9A-Za-z]，那就是特殊字符
print(res1)
# 分割字符串
